package se.joakimback.decorator.view;

import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import se.joakimback.decorator.model.Layer;
import se.joakimback.decorator.renderer.RenderManager;

public class LayerConfiguration extends Panel implements
		LayerSelectionListener, ChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3071627383398945322L;

	private Layer mLayer;

	private JSlider mRepeatSlider;

	private JSlider mDensitySlider;

	private JSlider mBaseScaleSlider;

	private JSlider mScaleVarianceSlider;

	private JSlider mSpreadSlider;

	private JSlider mRotationVarianceSlider;
	
	private JLabel mImageIcon;

	private boolean isUpdating = false;
	
	private int toPercentage(double pScale) {
		return (int) (pScale * 100);
	}

	private double fromPercentage(int pPercentage) {
		return (double) pPercentage / 100;
	}
	
	private void update() {
		isUpdating = true;
		
		mRepeatSlider.setValue(mLayer.getRepeat());
		mDensitySlider.setValue(mLayer.getDensity());
		mBaseScaleSlider.setValue(toPercentage(mLayer.getBaseScale()));
		mScaleVarianceSlider.setValue(toPercentage(mLayer.getScaleVariance()));
		mSpreadSlider.setValue(toPercentage(mLayer.getSpread()));
		mRotationVarianceSlider.setValue(toPercentage(mLayer
				.getRotationVariance()));
		mImageIcon.setIcon(new ImageIcon(mLayer.getImage()));
		
		isUpdating = false;
	}

	private void init() {
		mImageIcon = ComponentFactory.newIcon("Image");
		mRepeatSlider = ComponentFactory.newSlider(1, 10);
		mDensitySlider = ComponentFactory.newSlider(1, 20);
		mBaseScaleSlider = ComponentFactory.newSlider(1, 500);
		mScaleVarianceSlider = ComponentFactory.newSlider(0, 200);
		mSpreadSlider = ComponentFactory.newSlider(0, 100);
		mRotationVarianceSlider = ComponentFactory.newSlider(0, 100);
		
		mImageIcon.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				
			    final JFileChooser fc = new JFileChooser();
			    
			    if (fc.showOpenDialog(mImageIcon) == JFileChooser.APPROVE_OPTION) {
			    	fc.getSelectedFile();
			    	
			    	try {
				    	BufferedImage vImage = 
				    		ImageIO.read(fc.getSelectedFile());
				    	
				    	mLayer.setImage(vImage);
				    	
				    	update();
				    	stateChanged(null);
			    	} catch (Exception e2) {
			    		e2.printStackTrace();
			    	}
			    }
			}
		});
		
		mRepeatSlider.addChangeListener(this);
		mDensitySlider.addChangeListener(this);
		mBaseScaleSlider.addChangeListener(this);
		mScaleVarianceSlider.addChangeListener(this);
		mSpreadSlider.addChangeListener(this);
		mRotationVarianceSlider.addChangeListener(this);
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(ComponentFactory.newLabel("Image"));
		add(mImageIcon);
		
		add(ComponentFactory.newLabel("Repeat"));
		add(mRepeatSlider);

		add(ComponentFactory.newLabel("Density"));
		add(mDensitySlider);

		add(ComponentFactory.newLabel("Base Scale"));
		add(mBaseScaleSlider);

		add(ComponentFactory.newLabel("Scale Var."));
		add(mScaleVarianceSlider);

		add(ComponentFactory.newLabel("Spread"));
		add(mSpreadSlider);

		add(ComponentFactory.newLabel("Rotation"));
		add(mRotationVarianceSlider);
	}

	public LayerConfiguration() {
		super();
		
		init();
	}

	@Override
	public void layerSelected(Layer layer) {
		mLayer = layer;

		update();
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if (mLayer == null || isUpdating) return;
		
		mLayer.setRepeat(mRepeatSlider.getValue());
		mLayer.setDensity(mDensitySlider.getValue());
		mLayer.setBaseScale(fromPercentage(mBaseScaleSlider.getValue()));
		mLayer.setScaleVariance(fromPercentage(mScaleVarianceSlider.getValue()));
		mLayer.setSpread(fromPercentage(mSpreadSlider.getValue()));
		mLayer.setRotationVariance(fromPercentage(mRotationVarianceSlider.getValue()));
		
		RenderManager.getSingleton().request();
	}
}
